﻿using FineUI;
using System;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Common;
using ZHDJ.PersonalCenter.BLL;
using ZHDJ.WebPage;

namespace ZHDJ.PersonalCenter.Module.PartyCare
{
    public partial class CareInfoCourierEdit : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "";
            }
        }
        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                PartyArticle article = DB.PartyArticles.Find(id);
                if (article == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                txtTitle.Text = article.Title;
                ppBranch.Value = article.BranchID;
                ppBranch.Text = ControlBusiness.GetBranchPickerText(article.BranchID);
                dpActivityTime.Text = Convert.ToDateTime(article.ActivityBeginTime).ToString("yyyy-MM-dd");
                heContent.Text = article.Content;

                if (article.Status == 4)
                {
                    btnSaveClose.Visible = btnPublish.Visible = false;
                }
            }
            else
            {
                ppBranch.Value = this.BranchID;
                ppBranch.Text = ControlBusiness.GetBranchPickerText(ppBranch.Value);
            }
        }
        #endregion

        #region Events

        private void SaveData(bool isPublish)
        {
            string id = GetQueryIntValue("id");
            string imageUrl = string.Empty;

            if (id != "")
            {
                //更新
                PartyArticle article = DB.PartyArticles.Find(id);
                if (article == null)
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                article.Title = txtTitle.Text.Trim();
                article.BranchID = ppBranch.Value;
                article.Status = isPublish ? 4 : 1;
                article.Content = heContent.Text;
                article.ActivityBeginTime = Convert.ToDateTime(dpActivityTime.Text);
                article.ActivityEndTime = Convert.ToDateTime(dpActivityTime.Text);
                article.ModifyTime = DateTime.Now;
                article.ModifyBy = CurrentUser.LoginID;

                DB.SaveChanges();
            }
            else
            {
                PartyArticle article = new PartyArticle();
                article.ID = StringHelper.NewID;
                article.Title = txtTitle.Text.Trim();
                article.Module = "partylove";
                article.Category = "CareInfoCourier";
                article.BranchID = ppBranch.Value;
                article.ActivityBeginTime = Convert.ToDateTime(dpActivityTime.Text);
                article.ActivityEndTime = Convert.ToDateTime(dpActivityTime.Text);
                article.Status = isPublish ? 4 : 1;
                article.Content = heContent.Text;
                article.CreateTime = DateTime.Now;
                article.ModifyTime = DateTime.Now;
                article.CreateBy = CurrentUser.LoginID;
                article.ModifyBy = CurrentUser.LoginID;

                DB.PartyArticles.Add(article);

                DB.SaveChanges();
            }
        }

        /// <summary>
        /// 保存并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData(false);

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        protected void btnPublish_Click(object sender, EventArgs e)
        {
            SaveData(true);

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        protected void fileImage_FileSelected(object sender, EventArgs e)
        {
            var rs = UpFileBusiness.Upload(fileImage, CurrentUser.LoginID);
            if (rs.IsSuccess)
            {
                hdImageId.Text = rs.FileID;
                HyperLink1.ImageID = rs.FileID;
            }
        }
        #endregion
    }
}